﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
using System.Data.SqlClient;

/// <summary>
///DBOperate 的摘要说明
///更新日期：2012年7月20日
///用于风险管理系统数据库操作
/// </summary>
public class DBOperate
{
    private OleDbCommand dbCMD;
    private OleDbConnection dbConn;
    private OleDbTransaction dbTrans;
    private OleDbDataReader dbReader;
    
	public DBOperate()//string MyMapPath)
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
        
        ConnectDB();

	}
    private void ConnectDB()
    {
        string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;
                Data Source=" + System.Web.HttpContext.Current.Server.MapPath(@"~\App_Data\RiskManage.mdb");
        dbConn = new OleDbConnection(connStr);
        dbConn.Open();
        dbCMD = new OleDbCommand();
        dbCMD.Connection = dbConn;

    }

    public void SetCommandString(string sqlCMD)
    {
        dbCMD.CommandText = sqlCMD;
    }

    public void ExecuteNonReader()
    {
        dbCMD.ExecuteNonQuery();
        dbConn.Close();
    }

    public OleDbDataReader ExecuteReader()
    {
        dbReader = dbCMD.ExecuteReader();
        return dbReader;
    }

//by zhenqi
//@2012-08-13
    public int ExecuteScalar()
    {
        return Convert.ToInt32(dbCMD.ExecuteScalar());
    }
    
    public OleDbCommand GetDBCMD()
    {
        dbCMD = new OleDbCommand();
        dbCMD.Connection = dbConn;
        if (dbTrans != null)
            dbCMD.Transaction = dbTrans;
        return dbCMD;
    }

    public void CloseDB()
    {
        if (dbConn.State == ConnectionState.Open)
            dbConn.Close();
    }

    public void BeginTrans()
    {
        dbTrans = dbConn.BeginTransaction();
    }

    public void TransCommit()
    {
        dbTrans.Commit();
    }

    public void TransRollBack()
    {
        dbTrans.Rollback();
    }
}

///<summary>
///用于操作172.16.51.49上的服务器
///</summary>
public class jwbDBOperate
{
    SqlConnection myConn;
    SqlDataReader dbRedaer;
    SqlCommand dbCmd;
	public jwbDBOperate(string connStr)
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
        myConn = new SqlConnection();
        myConn.ConnectionString = connStr;
        myConn.Open();
        dbCmd = new SqlCommand();
        dbCmd.Connection = myConn;
	}
    //设置查询数据库字符
    public void SetCommandString(string cmd)
    {
        dbCmd.CommandText = cmd;
    }
    public void ExecuteNonReader()
    {
        dbCmd.ExecuteNonQuery();
        myConn.Close();
    }
    public SqlDataReader ExecuteReader()
    {
        dbRedaer = dbCmd.ExecuteReader();
        return dbRedaer;
    }
    public void ClodDBConn()
    {
        myConn.Close();
    }
}
